package com.ctshk.rpc.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.system.dto.SysMessageDTO;
import com.ctshk.rpc.system.dto.mian.data.MainDataTravelTipsDTO;
import com.ctshk.rpc.system.entity.MainDataTravelTips;
import com.ctshk.rpc.system.entity.SystemMessage;
import com.ctshk.rpc.system.mapper.MainDataTravelTipsMapper;
import com.ctshk.rpc.system.req.main.data.MainDataTravelTipsIdReq;
import com.ctshk.rpc.system.req.main.data.MainDataTravelTipsQueryReq;
import com.ctshk.rpc.system.req.main.data.MainDataTravelTipsReq;
import com.ctshk.rpc.system.service.IMainDataTravelTipsService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 旅游小费规则 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-01-26
 */
@DubboService
public class MainDataTravelTipsServiceImpl extends ServiceImpl<MainDataTravelTipsMapper, MainDataTravelTips> implements IMainDataTravelTipsService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(MainDataTravelTipsReq mainDataTravelTipsReq, TokenUser tokenUser) {
        MainDataTravelTips mainDataTravelTips = new MainDataTravelTips();
        BeanUtils.copyProperties(mainDataTravelTipsReq, mainDataTravelTips);
        mainDataTravelTips.setIsDeleted(IsDeletedCode.NO.getCode());
        mainDataTravelTips.setCreateId(tokenUser.getId());
        mainDataTravelTips.setGmtCreate(LocalDateTime.now());
        mainDataTravelTips.setModifiedId(tokenUser.getId());
        mainDataTravelTips.setGmtModified(LocalDateTime.now());
        mainDataTravelTips.setModifiedName(tokenUser.getNickname());
        baseMapper.insert(mainDataTravelTips);
    }

    @Override
    public PageResponse<MainDataTravelTipsDTO> queryList(MainDataTravelTipsQueryReq mainDataTravelTipsQueryReq, TokenUser tokenUser) {
        IPage<MainDataTravelTips> page = new Page<MainDataTravelTips>(mainDataTravelTipsQueryReq.getPageNo(), mainDataTravelTipsQueryReq.getPageSize());
        QueryWrapper<MainDataTravelTips> queryWrapper = new QueryWrapper<MainDataTravelTips>();
        queryWrapper.lambda().eq(MainDataTravelTips::getIsDeleted, IsDeletedCode.NO.getCode());
        queryWrapper.lambda().eq(null != mainDataTravelTipsQueryReq.getStatus(),MainDataTravelTips::getStatus, mainDataTravelTipsQueryReq.getStatus());
        queryWrapper.lambda().eq(null != mainDataTravelTipsQueryReq.getType(),MainDataTravelTips::getTipsType, mainDataTravelTipsQueryReq.getType());
        queryWrapper.lambda().like(StringUtils.isNotBlank(mainDataTravelTipsQueryReq.getTipsName()),MainDataTravelTips::getTipsName, mainDataTravelTipsQueryReq.getTipsName());
        queryWrapper.lambda().orderByDesc(MainDataTravelTips::getGmtModified);
        IPage<MainDataTravelTips> iPage = baseMapper.selectPage(page, queryWrapper);
        List<MainDataTravelTips> mainDataTravelTipsList = iPage.getRecords();
        if (CollectionUtils.isEmpty(mainDataTravelTipsList)) {
            return new PageResponse<>(new ArrayList<>(), iPage.getCurrent() == 1, iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize());
        }
        List<MainDataTravelTipsDTO> mainDataTravelTipsDTOList = EntityUtil.copyList(mainDataTravelTipsList, MainDataTravelTipsDTO.class);
        return new PageResponse<>(mainDataTravelTipsDTOList, iPage.getCurrent() == 1, iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(MainDataTravelTipsIdReq mainDataTravelTipsIdReq, TokenUser tokenUser) {
        MainDataTravelTips mainDataTravelTips = EntityUtil.copy(mainDataTravelTipsIdReq, MainDataTravelTips.class);
        mainDataTravelTips.setModifiedId(tokenUser.getId());
        mainDataTravelTips.setGmtModified(LocalDateTime.now());
        mainDataTravelTips.setModifiedName(tokenUser.getUsername());
        mainDataTravelTips.setIsDeleted(IsDeletedCode.YES.getCode());
        baseMapper.updateById(mainDataTravelTips);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(MainDataTravelTipsReq mainDataTravelTipsReq, TokenUser tokenUser) {
        MainDataTravelTips mainDataTravelTips = EntityUtil.copy(mainDataTravelTipsReq, MainDataTravelTips.class);
        mainDataTravelTips.setModifiedId(tokenUser.getId());
        mainDataTravelTips.setGmtModified(LocalDateTime.now());
        mainDataTravelTips.setModifiedName(tokenUser.getUsername());
        baseMapper.updateById(mainDataTravelTips);
    }

    @Override
    public void updateStatus(MainDataTravelTipsIdReq mainDataTravelTipsIdReq, TokenUser tokenUser) {
        MainDataTravelTips mainDataTravelTips = baseMapper.selectById(mainDataTravelTipsIdReq.getId());
        if (null == mainDataTravelTips) {
            throw new BusinessException(SystemError.SYS_411);
        }
        MainDataTravelTips travelTips = new MainDataTravelTips();
        if (mainDataTravelTips.getStatus() == 0) {
            travelTips.setId(mainDataTravelTipsIdReq.getId());
            travelTips.setStatus(1);
        }
        if (mainDataTravelTips.getStatus() == 1) {
            travelTips.setId(mainDataTravelTipsIdReq.getId());
            travelTips.setStatus(0);
        }
        baseMapper.updateById(travelTips);
    }


}
